package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.AlertCode;
import com.service.UserInfoMybatisServiceImpl;
import com.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;


@WebServlet({"/userOperate"})
public class UserMyBatisController
        extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session;
        Object user;
        Map map = MyUtil.parameterNamesUtil(req, resp);

        UserInfoMybatisServiceImpl userInfoMybatisServiceImpl = new UserInfoMybatisServiceImpl();

        AlertCode alertCode = null;

        switch ((new StringBuilder()).append(map.get("mothed")).append("").toString()) {
            case "show":

                alertCode = userInfoMybatisServiceImpl.userListPage(map);

                System.out.println(map);

                session = req.getSession();

                user = session.getAttribute("user");

                alertCode.setSessions(user);
                break;

            case "delete":

                alertCode = userInfoMybatisServiceImpl.userDelAll((new StringBuilder()).append(map.get("id")).append("").toString());
                break;

            case "update":

                System.out.println(map);

                alertCode = userInfoMybatisServiceImpl.userUpdate(map);
                break;


            case "updateKey":

                alertCode = userInfoMybatisServiceImpl.userKeyUpdate(Integer.parseInt((new StringBuilder()).append(map.get("id")).append("").toString()));
                break;

            case "add":

                alertCode = userInfoMybatisServiceImpl.userAdd(map);
                break;

            case "showOption":

                alertCode = userInfoMybatisServiceImpl.showOption(map);
                break;
        }


        System.out.println(alertCode);

        String str = JSON.toJSONString(alertCode);

        resp.setContentType("application/json:charset=utf-8");

        resp.getWriter().println(str);
    }
}
